package com.zkadmin.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.zkoss.zk.ui.Execution;

public abstract class CookieUtils {
	public static final String THEME_COOKIE_KEY = "zktheme";
	public static final String LOCAL_COOKIE_KEY = "zkLocal";

	public static void setLocal(Execution execution, String localName) {
		Cookie cookie = new Cookie(LOCAL_COOKIE_KEY, localName);
		cookie.setMaxAge(60 * 60 * 24 * 30);// store 30 days
		String cp = execution.getContextPath();
		if (cp.length() == 0) {
			cp = "/";
		}
		cookie.setPath(cp);
		((HttpServletResponse) execution.getNativeResponse()).addCookie(cookie);
	}

	public static String getLocal(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(LOCAL_COOKIE_KEY)) {
					return cookie.getValue();
				}
			}
		}
		return "zh_CN";
	}
}
